<?php

namespace App\Http\Controllers;

use App\Student;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Validator;

class TestController extends Controller
{

    //
    public function test(Request $request)
    {
        if($request->isMethod('post')){
            $id=$request->input('id');
            $res = Student::where('id','=',$id)->paginate(5);
            return view('test/student', ['res' => $res]);
        }else{
            $res = Student::paginate(5);
            return view('test/student', ['res' => $res]);
        }


    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')){
            $data = $request->input();
            //dd($data);
            $vali = Validator::make($request->input(), [
                'name' => 'required|min:2|max:10',
                'sex' => 'required',
                'age' => 'required|integer',
                'cla' => 'required|integer',
            ], [
                'required' => ':attribute 必填项',
                'integer' => ':attribute 必须为整型',
                'min' => ':attribute 姓名长度至少2',
                'max' => ':attribute 长度小于10',
            ], [
                'name' => '姓名',
                'sex'=>'性别',
                'age'=>'年龄',
                'cla'=>'班级',

            ]);
            if ($vali->fails()){
                return redirect()->back()->withErrors($vali)->withInput();
            }
            $res = Student::create([
                'name' => $data['name'],
                'sex' => $data['sex'],
                'age' => $data['age'],
                'cla' => $data['cla'],
            ]);
            if ($res) {
                return redirect('student')->with('success', '添加成功！');
            } else {
                return redirect()->back()->with('error', '添加失败！');
            }
        } else {
            return view('test/add');
        }


    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $res = DB::table('student')->where('id', $id)->delete();
        if ($res) {
            return redirect('student')->with('success', '删除成功！');
        } else {
            return redirect()->back()->with('error', '删除失败！');
        }

    }

    public function update(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->input();
            $id = $data['id'];
            //dd($data);
            $res = DB::table('student')->where('id', $id)->update([
                'name' => $data['name'],
                'sex' => $data['sex'],
                'age' => $data['age'],
                'cla' => $data['cla'],
            ]);
            if ($res) {
                return redirect('student')->with('success', '修改成功');
            } else {
                return redirect()->back()->with('error', '修改失败')->withInput();
            }
        } else {
            $id = $request->input('id');
            $res = DB::table('student')->find($id);
            dump($res);
            return view('test/update', ['res' => $res]);
        }

    }

    public function detail(Request $request)
    {
        //dd($id);
        $id = $request->input('id');
        $res = DB::table('student')->find($id);
        return view('test/detail', ['res' => $res]);
    }

}
